﻿#_*_ coding: UTF-8 _*_

from core import uicomponents, db
from PyQt4 import QtGui, QtCore
from ui_basic_classes import MainView,NiceTextEdit
import servers.quran.ui,servers.quran.definitions
from servers.page_number.definitions import  *
from classes import *

class QuranTranslationView(MainView):
	def __init__(self,parent=None):
		MainView.__init__(self,parent)
		self.translation = NiceTextEdit()

		llayout = QtGui.QVBoxLayout()
		llayout.addWidget(self.translation)

		self.setLayout(llayout)
		QtCore.QObject.connect(servers.quran.ui.quranBrowser.quran.soorehSelector, QtCore.SIGNAL('currentIndexChanged(int)'), self.transGotoSoorehZ)
		QtCore.QObject.connect(servers.quran.ui.quranBrowser.quran.ayeSelector, QtCore.SIGNAL('valueChanged(int)'), self.transGotoAye)
		QtCore.QObject.connect(servers.quran.ui.quranBrowser.quran.safheSelector, QtCore.SIGNAL('valueChanged(int)'), self.transGotoSafhe)
		QtCore.QObject.connect(servers.quran.ui.quranBrowser.quran.quran,QtCore.SIGNAL("selectionChanged()"),self.transCurrent)		
		self.sooreh = None
	def showCurrentTrans(self):
		showView()
		self.transCurrent()
	def transCurrent(self):
		if  uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].isVisible():
#			self.transGotoSoorehZ(uicomponents.SharedUi.selectionStartWord.aye.sure.id-1)
#			self.transGotoAye(uicomponents.SharedUi.selectionStartWord.aye.n)
			self.ayat = []
			self.ayat.append(uicomponents.SharedUi.selectionStartWord.aye)
			a = uicomponents.SharedUi.selectionStartWord.aye
			while not (a == uicomponents.SharedUi.selectionEndWord.aye):
				a = a.next()
				self.ayat.append(a)
			soorehMatn = QtCore.QString()
			for aye in self.ayat:
				soorehMatn.append(u'%s<b>(%d)</b><a name="X%d"></a> ' % (aye.translation.fa.decode('utf8'), aye.n, aye.n))
			self.translation.setHtml('<div dir=rtl style="font-family: B Nazanin; font-size:14pt">'+soorehMatn+'</div>')

	def transShowSooreh(self, sooreh):
		realAyat = list(sooreh.ayat)
		self.ayat = [aye.translation for aye in realAyat]
		soorehMatn = QtCore.QString()
		counter = 1
		for aye in self.ayat:
			soorehMatn.append(u'%s<b>(%d)</b><a name="X%d"></a> ' % (aye.fa.decode('utf8'), counter, counter))
			counter = counter + 1
		self.translation.setHtml('<div dir=rtl style="font-family: B Nazanin; font-size:14pt">'+soorehMatn+'</div>')

	def transGotoSoorehZ(self, soorehNumber):
		self.sooreh = Quran.getSoorehByNumber(soorehNumber+1)
		self.transShowSooreh(self.sooreh)

	def transGotoSafhe(self, number):
		safhe = page_query.get(number)
		targetSooreh = safhe.page_ayat[0].aye.sure
		if targetSooreh is not self.sooreh:
			idx = targetSooreh.id-1
			self.transGotoSoorehZ(idx)

		self.transGotoAye(safhe.page_ayat[0].aye.n)
		
	def transGotoAye(self, numberInSooreh):
		self.translation.scrollToAnchor(u'X%d' % numberInSooreh)

def showView():
	if not uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].isVisible():
		uicomponents.SharedUi.mainWindow.addDockWidget(QtCore.Qt.BottomDockWidgetArea,uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'])
		uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].show()
	
uicomponents.SharedUi.applications['TranslationFooladvand']  = {}
uicomponents.SharedUi.applications['TranslationFooladvand']['actionSlot'] = showView
uicomponents.SharedUi.applications['TranslationFooladvand']['name']  = u'ترجمه آقاي فولادوند'
uicomponents.SharedUi.applications['TranslationFooladvand']['shortcut'] = u'Ctrl+T'
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']={}
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'] = QtGui.QDockWidget(uicomponents.SharedUi.mainWindow)
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].setWindowTitle(u'ترجمه آقاي فولادوند')
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].resize(500,100)
quranTranslationFoolad = QuranTranslationView()
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].setWidget(quranTranslationFoolad)
uicomponents.SharedUi.applications['TranslationFooladvand']['docables']['tr'].hide()

showAyeTranslation = QtGui.QAction(u'نمايش معنا', None)
showAyeTranslation.setShortcut('Ctrl+T')
uicomponents.SharedUi.wordPopupMenu.addAction(showAyeTranslation)
uicomponents.SharedUi.ayePopupMenu.addAction(showAyeTranslation)

uicomponents.SharedUi.modulesListMenu.addAction(showAyeTranslation)

QtCore.QObject.connect(showAyeTranslation, QtCore.SIGNAL('triggered()'), quranTranslationFoolad.showCurrentTrans)


def changeAdvanceSearchQueryTranslation(text):
	query = unicode(servers.quran.definitions.advSearchQuery['widget'].text())
	tindex = query.find("translation{")
	if tindex>-1:
		if len(unicode(text).strip())==0:
			query = query[:tindex]+query[tindex+query[tindex:].find("}")+1:]
		else:	
			query = query[:tindex] + "translation{" + text + "}" + query[tindex+query[tindex:].find("}")+1:]
	else:
		query += " translation{" + text + "} "
	servers.quran.definitions.advSearchQuery['widget'].setText(query)

servers.quran.definitions.advSearchPlugins['translation'] = {}
servers.quran.definitions.advSearchPlugins['translation']['layout'] = QtGui.QVBoxLayout()
servers.quran.definitions.advSearchPlugins['translation']['layout'].addWidget(QtGui.QLabel(u'داراي ترجمه'))
translationAdvancedSearchQuery = QtGui.QLineEdit()
servers.quran.definitions.advSearchPlugins['translation']['layout'].addWidget(translationAdvancedSearchQuery)
QtCore.QObject.connect(translationAdvancedSearchQuery,QtCore.SIGNAL("textChanged(QString)"),changeAdvanceSearchQueryTranslation)

